﻿const express = require('express');
const router = express.Router();
const multer = require('multer');
const ScoreSubItemModel = require('../models/score-sub-item');
const createGuid = require('../lib/util').createGuid;
const get_client_ip = require('../lib/util').get_client_ip;
const getCurrentUserGuid = require('../lib/util').getCurrentUserGuid;


// 获取字典表中的所有的成绩子项列表
router.get('/dlist/', function(req, res, next) {
    ScoreSubItemModel.getDList(function(status, vals){
        res.send({ status: status, data: vals });
    });
});

// 获取某个成绩环节的所有的自定义的成绩子项列表
router.get('/userdefinelist/', function(req, res, next) {
    let classGuid = req.query.classGuid;
    let scoreSegmentGuid = req.query.scoreSegmentGuid;
    ScoreSubItemModel.getUserDefineList(classGuid, scoreSegmentGuid, function(status, vals){
        res.send({ status: status, data: vals });
    });
});

// 获取某个成绩环节的所有的成绩子项列表
router.get('/list/:scoreSegmentGuid', function(req, res, next) {
    let scoreSegmentGuid = req.params.scoreSegmentGuid;
    ScoreSubItemModel.getList(scoreSegmentGuid, function(status, vals){
        res.send({ status: status, data: vals });
    });
});

// 添加某个班的成绩环节与成绩子项的联系(暂时不用)
router.post('/create', function(req, res, next){
    let scoreSegmentScoreSubItemGuid = createGuid();
    let currentUserGuid = getCurrentUserGuid(req);
    let ip = get_client_ip();
    ScoreSubItemModel.create(scoreSegmentScoreSubItemGuid, req.body, currentUserGuid, ip, function(status, vals){
        if (status) res.send({ status: status, data: vals });
        else res.send({ status: status, err: vals });
    });
});

// 添加某个班的成绩环节与自定义的成绩子项的联系
router.post('/createuserdefinesubitem', function(req, res, next){
    let scoreSegmentScoreSubItemGuid = createGuid();
    let currentUserGuid = getCurrentUserGuid(req);
    let ip = get_client_ip();
    ScoreSubItemModel.createUserDefineSubItem(scoreSegmentScoreSubItemGuid, req.body, currentUserGuid, ip, function(status, vals){
        if (status) res.send({ status: status, data: vals });
        else res.send({ status: status, err: vals });
    });
});

// 添加某个班的成绩环节与成绩子项的联系
router.post('/createList', function(req, res, next){
    let currentUserGuid = getCurrentUserGuid(req);
    let ip = get_client_ip();
    ScoreSubItemModel.createList(req.body, currentUserGuid, ip, function(status, vals){
        if (status) res.send({ status: status, data: vals });
        else res.send({ status: status, err: vals });
    });
});

// 删除某个班的成绩环节与成绩子项的联系
router.delete('/:scoreSubItemGuid', function(req, res, next){
    let scoreSubItemGuid = req.params.scoreSubItemGuid;
    let scoreSegmentGuid = req.query.scoreSegmentGuid;
    let currentUserGuid = getCurrentUserGuid(req);
    let ip = get_client_ip();
    ScoreSubItemModel.delete(scoreSegmentGuid, scoreSubItemGuid, currentUserGuid, ip, function(status, vals){
        if (status) res.send({ status: status, data: 'ok' });
        else res.send({ status: status, err: vals });
    });
});

// 删除某个班的成绩环节与成绩子项的联系（批量）
router.delete('/list', function(req, res, next){
    let currentUserGuid = getCurrentUserGuid(req);
    let ip = get_client_ip();
    ScoreSubItemModel.deleteList(req.body, currentUserGuid, ip, function(status, vals){
        if (status)
        {
            res.send({ status: status, data: 'ok' });
        }
        else res.send({ status: status, err: vals });
    });
});

// 改变某个班的成绩环节与成绩子项的联系（批量）
router.put('/changelist/:id', function(req, res, next){
    let classGuid = req.params.id;
    let currentUserGuid = getCurrentUserGuid(req);
    let ip = get_client_ip();
    // 批量添加
    if (req.body[0].length > 0)
    {
        ScoreSubItemModel.createList(classGuid, req.body[0], currentUserGuid, ip, function(statusAdd, valsAdd){
            if (statusAdd) {
                if (req.body[1].length > 0)
                    {
                    ScoreSubItemModel.deleteList(classGuid, req.body[1], currentUserGuid, ip, function(statusDelete, valsDelete){
                        if (statusDelete) res.send({ status: statusDelete, data: valsDelete });
                        else res.send({ status: statusDelete, err: valsDelete });
                    });
                }
                else
                {
                    res.send({ status: statusAdd, data: valsAdd });
                }
            }
            else res.send({ status: statusAdd, err: valsAdd });
        });
        
    }
    else if (req.body[1].length > 0)
    {
        ScoreSubItemModel.deleteList(classGuid, req.body[1], currentUserGuid, ip, function(status, vals){
            if (status) res.send({ status: status, data: vals });
            else res.send({ status: status, err: vals });
        });
    }
    else
    {
        res.send({status: 1, data: {} });
    }
});

// 保存权重
router.post('/saveweight', function(req, res, next){
    let ip = get_client_ip();
    ScoreSubItemModel.saveWeight(req.body, ip, function(status, vals) {
        if (status) res.send({ status: status, data: vals });
        else res.send({ status: status, err: vals });
    });
});

module.exports = router;